********************************************************************************
*** Fraction of payments made by day of week

use "data/cleaned/hugo/All balance actions.dta", clear

* Generate index for day of the week
gen weekday = dow(dofc(timestamp))

*histograms for day and week when coverage is added manually
tab source, mi
keep if source == "manual_sms" | source == "manual_web" | source == "initial_bind"
replace source = "refill" if inlist(source, "manual_sms","manual_web")

sort source weekday
gen count = 1
by source weekday: egen tot = sum(count)
by source weekday: keep if _n==1

*get fraction of all payments
egen totot = sum(tot)
gen totfrac = tot/totot

keep source weekday totfrac
reshape wide totfrac, i(weekday) j(source) string

label variable totfracrefill "Refill"
label variable totfracinitial_bind "Initial Bind"

gen totfrac = totfracinitial_bind + totfracrefill

foreach var of varlist totfr* {
		replace `var' = 100*`var'
	}

graph bar totfracinitial_bind totfracrefill, ///
	over(weekday, ///
		relabel(1 "Sun" 2 "Mon" 3 "Tues" 4 "Wed" 5 "Thurs" 6 "Fri" 7 "Sat")) stack ///
		blabel(total, format(%4.2f) position(outside) gap(*.3) color(black) size(medsmall)) ///
		legend(pos(11) ring(0) rows(3) label(1 "Initial Purchase") label(2 "Refill")) ///
	subtitle("Share of Purchase Amounts by Day of the Week", position(11) span)
graph export "output/figures/purchases by day of the week.pdf", replace

